package cn.edu.zhku.citrusmonitor.announcement.controller;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.edu.zhku.citrusmonitor.announcement.service.AnnouncementService;
import cn.edu.zhku.citrusmonitor.announcement.service.AnnouncementServiceImpl;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbAnnouncement;
import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.PageBean;
import net.sf.json.JSONObject;

@WebServlet("/announce.do")
public class AnnouncementServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
	AnnouncementService ans=new AnnouncementServiceImpl();
	
	public JSONObject addAnnounce(HttpServletRequest request, HttpServletResponse response){
		try {
			TbAnnouncement tan= new TbAnnouncement();
	
			BeanUtils.populate(tan, request.getParameterMap());//这里要有	title,content
			
			
			ans.addAnnounce(tan);
			return JSONObject.fromObject(new Message("1", "添加公告成功"));
			
		
		} catch (Exception e) {
			return JSONObject.fromObject(new Message("2", "添加公告失败"));	
		}
	}

	public JSONObject removeAnnounce(HttpServletRequest request, HttpServletResponse response){
		String announcement_id=request.getParameter("announcementId");
		
		try {
			if(ans.removeAnnounce(announcement_id)==1)
			{
				return JSONObject.fromObject(new Message("1", "删除公告成功"));
			}
			return JSONObject.fromObject(new Message("2", "删除公告失败"));
		} catch (SQLException e) {
			
			return JSONObject.fromObject(new Message("2", "删除公告失败"));
		}	
	}
	
	
	public JSONObject modifyAnnounce(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException, SQLException{
		
			TbAnnouncement tan= new TbAnnouncement();
	
			BeanUtils.populate(tan, request.getParameterMap());//这里要有	title,content,annoucement_id
			
			if(ans.modifyAnnounce(tan)==1)
			{
				return JSONObject.fromObject(new Message("1", "修改公告成功"));
			}
			return JSONObject.fromObject(new Message("2", "修改公告失败"));
	
	}
	
	//根据id查找announcement
	public String showAnById(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		String announcementId=request.getParameter("id");
		TbAnnouncement announce=ans.showAnById(announcementId);
		
		Bean bean=new Bean();
		bean.setContent(announce.getContent());
		bean.setId(announce.getAnnouncementId());
		bean.setTitle(announce.getTitle());
		bean.setTime(announce.getCreateTime());
		bean.setDistribution("仲恺柑橘网");

		
		return JSON.toJSONStringWithDateFormat(bean, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	
	
	
	
	
	//ajax搜索框
	public String findAnByWord(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
			String word=request.getParameter("word");
			List<TbAnnouncement> list = new ArrayList<TbAnnouncement>();
			list=ans.findAnByWord(word);
			
			if(list.size()!=0) {
				return JSONArray.toJSONString(list);				
			}
			
			return JSON.toJSONString(new Message("2", "无此结果"));
		
	}
	
	
	//首页显示前几条问题
		public String showAnnounce(HttpServletRequest request, HttpServletResponse response) throws SQLException {
				int pageSize=0;
				String pageSize1=request.getParameter("pageSize");
				if(pageSize1!=null) {
					pageSize = Integer.parseInt(pageSize1);
				}
				else {
					pageSize=4;
				}
				
				List<TbAnnouncement> announce = ans.showAnnounce(pageSize);
				return JSON.toJSONStringWithDateFormat(announce, "yyyy-MM-dd", SerializerFeature.EMPTY);				
		}
	
	//后台管理，显示全部
		public String showAll(HttpServletRequest request, HttpServletResponse response) throws SQLException {
			
			int currentPage=0;
			String currentPage1=request.getParameter("currentPage");
			if(currentPage1!=null) {
				currentPage = Integer.parseInt(currentPage1);
			}
			else {
				currentPage=4;
			}
			
			int pageSize=0;
			String pageSize1=request.getParameter("pageSize");
			if(pageSize1!=null) {
				pageSize = Integer.parseInt(pageSize1);
			}
			else {
				pageSize=4;
			}
			
			PageBean<TbAnnouncement> pb=new PageBean<TbAnnouncement>();
			pb=ans.showAll(currentPage,pageSize);			
			return JSON.toJSONString(pb);
			
		}
		//批量删除
		public JSONObject delAll(HttpServletRequest request, HttpServletResponse response) throws SQLException{
			String[] ids = request.getParameterValues("ids");
			
			
				if(ans.delAll(ids)==1)
				{
					return JSONObject.fromObject(new Message("1", "删除公告成功"));
				}
				return JSONObject.fromObject(new Message("2", "删除公告失败"));
			
		}
		
		
		
		
	
}
